package run.siyuan.manag.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import run.siyuan.common.entity.Result;
import run.siyuan.common.exception.ExceptionProperties;
import run.siyuan.manag.annotation.OperLog;
import run.siyuan.manag.entity.SysDict;
import run.siyuan.manag.entity.SysUser;
import run.siyuan.manag.entity.vo.SysUserVo;
import run.siyuan.manag.service.SysUserService;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author siyuan
 * @since 2021-01-13
 */
@Slf4j
@Api(tags = "用户管理")
@RestController
@RequestMapping("/manag/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService userService;
    @Autowired
    private ExceptionProperties properties;

    @OperLog(description = "字典新增或修改")
    @PreAuthorize("hasAnyAuthority('sys:user:add') AND hasAnyAuthority('sys:user:edit')")
    @ApiOperation("新增或修改")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SysUser sysUser) {
        boolean result = false;
        if (StrUtil.isBlank(sysUser.getId())) {
            result = userService.save(sysUser);
        } else {
            result = userService.updateById(sysUser);
        }
        if (!result) {
            return Result.error(50000, properties.getCodeName("50000"));
        }
        return Result.success();
    }

    @OperLog(description = "字典详情")
    @PreAuthorize("hasAnyAuthority('sys:user:view')")
    @ApiOperation(value = "详情", response = SysUser.class)
    @GetMapping("/getOne/{id}")
    public Result getDictById(@PathVariable Long id) {
        if (ObjectUtil.isEmpty(id)) {
            return Result.error(50001, properties.getCodeName("50001"));
        }
        return Result.success().data(userService.getById(id));
    }


    @OperLog(description = "字典列表")
    @PreAuthorize("hasAnyAuthority('sys:user:view')")
    @ApiOperation(value = "列表", response = SysDict.class)
    @GetMapping("/list")
    public Result list() {
        return Result.success().data(userService.list());
    }

    @OperLog(description = "字典分页")
    @PreAuthorize("hasAnyAuthority('sys:user:view')")
    @ApiOperation(value = "分页", response = SysUser.class)
    @PostMapping("/page")
    public Result page(@RequestBody SysUserVo sysUserVo) {
        try {
            Page<SysUser> requestPage = new Page<>(sysUserVo.getPage(), sysUserVo.getPageLimit());
            QueryWrapper<SysUser> wrapper = new QueryWrapper();
            if (StrUtil.isNotBlank(sysUserVo.getName())) {
                wrapper.like("name", sysUserVo.getName());
            }
            return new Result().data(userService.page(requestPage, wrapper));
        } catch (Exception e) {
            return Result.error(50000, properties.getCodeName("50000") + e.getMessage());
        }
    }

    @OperLog(description = "字典删除")
    @PreAuthorize("hasAnyAuthority('sys:user:delete')")
    @ApiOperation("批量删除")
    @DeleteMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<String> idList) {
        if (!userService.removeByIds(idList)) {
            return Result.error(50001, properties.getCodeName("50001"));
        }
        return Result.success();
    }

    // TODO 用户权限
    // TODO 用户角色


}

